﻿using Apache.NMS;
using HAActiveMQ.Interface;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HAActiveMQ.Entity
{
    public class MqProducer : BaseMq
    {
        /// <summary>
        /// 默认参数方便用户进行软件测试
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <param name="user"></param>
        /// <param name="password"></param>
        public MqProducer(string ipAddress = "activemq:failover:(tcp://127.0.0.1:61616)", string user = "admin", string password = "admin")
        {
            Url = ipAddress;
            factory = new NMSConnectionFactory(Url);
            connection = factory.CreateConnection(user, password);
            connection.Start();
        }
        /// <summary>
        /// 发送Queue消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerQueue(string content, string name = "")
        {
            if (string.IsNullOrWhiteSpace(content)) return;
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetQueue(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateTextMessage(content));
            });
        }
        /// <summary>
        /// 发送Queue消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerQueue<TEntity>(TEntity content, string name = "")
            where TEntity : IMq, new()
        {
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetQueue(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateObjectMessage(content));
            });
        }
        /// <summary>
        /// 发送Topic消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerTopic(string content, string name = "")
        {
            if (string.IsNullOrWhiteSpace(content)) return;
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetTopic(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateTextMessage(content));
            });
        }
        /// <summary>
        /// 发送Topic消息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        public async Task ProducerTopic<TEntity>(TEntity content, string name = "")
            where TEntity : IMq, new()
        {
            if (string.IsNullOrWhiteSpace(name)) name = this.Name;
            if (string.IsNullOrWhiteSpace(name)) return;

            await Task.Run(() =>
            {
                ISession session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
                IDestination dest = session.GetTopic(name);
                IMessageProducer producer = session.CreateProducer(dest);
                producer.DeliveryMode = MsgDeliveryMode.NonPersistent;
                producer.Send(session.CreateObjectMessage(content));
            });
        }
    }
}
